package com.zhuss.product.processor;

import java.util.*;

import com.zhuss.bean.vo.product.line.ListLineSiteVo;
import com.zhuss.enums.SiteType;
import com.zhuss.product.db.dao.ext.LineSiteDao;
import com.zhuss.product.processor.cache.SiteCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.site.UpdateSiteBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.product.db.dao.ext.SiteDao;
import com.zhuss.product.db.entity.Site;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * @description: 站点修改
 * @author: zhushanshuo
 * @create: Wed Dec 30 08:43:41 CST 2020
 **/
@Component("siteUpdate")
public class SiteUpdate extends AbstractProcessor<Object, UpdateSiteBo> implements Processor<Object, UpdateSiteBo> {

    @Autowired
    private SiteDao siteDao;

    @Autowired
	@Qualifier("siteRead")
	private SiteRead siteRead;

	@Autowired
	@Qualifier("siteCache")
	private SiteCache siteCache;

	@Autowired
	private LineSiteDao lineSiteDao;

    /**
     * 校验
     */
    @Override
    public void check(UpdateSiteBo bo) throws DzcxException {
    	Long siteId = bo.getSiteId();
    	String name = bo.getName();
    	if (siteId == null || siteId <= 0l) {
    		throw new DzcxException(ExceptionCode.站点ID为空);
    	}
		if (bo.getLng() == null || "".equals(bo.getLng().trim())) {
			throw new DzcxException(ExceptionCode.经度为空);
		}

		if (bo.getLat() == null || "".equals(bo.getLat().trim())) {
			throw new DzcxException(ExceptionCode.纬度为空);
		}

    	if (name == null || "".equals(name.trim())) {
    		 throw new DzcxException(ExceptionCode.站点名称为空);
    	}
		Site detail = siteDao.selectByPrimaryKey(bo.getSiteId());
    	if (!name.equals(detail.getName())) {
			List<ListLineSiteVo> siteVos = lineSiteDao.listBySiteId(siteId);
			if (siteVos.size() > 0) {
				throw new DzcxException(ExceptionCode.站点在使用中);
			}
		}

    	List<Site> list = siteRead.listNotId(name, siteId);
		String lng = bo.substring(bo.getLng());
		String lat = bo.substring(bo.getLat());

		for (Site site : list) {
			if ((bo.substring(site.getLng()).equals(lng)) && (bo.substring(site.getLat()).equals(lat))
					&& (site.getName().equals(name))) {
				throw new DzcxException(ExceptionCode.站点已经存在);
			}
		}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(UpdateSiteBo param) {
    	Site record = new Site();
    	record.setSiteId(param.getSiteId());
    	record.setType(param.getType());
    	record.setName(param.getName());
    	record.setAddress(param.getAddress());
    	record.setLng(param.getLng());
    	record.setLat(param.getLat());
    	record.setDirection(param.getDirection());
    	record.setInfo(param.getInfo());
    	record.setStatus(param.getStatus());
    	record.setSort(param.getSort());

		if (param.getType() != null) {
			record.setSiteNo(SiteType.getTag(param.getType()) + record.getSiteId());
		}
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	siteDao.updateByPrimaryKeySelective(record);

    	siteCache.clear(record.getSiteId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateSiteBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.站点修改.key());
    	record.setTargetId(param.getSiteId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
